home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Contributed / SpriteWorld / SpriteWorld Examples / SpriteTest / About.c next >
Encoding:
Text File  |  2000-10-06  |  9.3 KB  |  397 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. //    About.c
  3. //
  4. //    By:        Tony Myles
  5. //
  6. //    Copyright: © 1993-94 Tony Myles, All rights reserved worldwide.
  7. ///--------------------------------------------------------------------------------------
  8.  
  9. #include <Fonts.h>    
  10. #include <TextUtils.h>
  11.  
  12. #include <SWIncludes.h>            // Automatically include all SpriteWorld.h files
  13. #include <SWApplication.h>    
  14.  
  15. #ifndef __DIALOGUTILS__
  16. #include <SWDialogUtils.h>
  17. #endif
  18.  
  19. #ifndef __SPRITETEST__
  20. #include "SpriteTest.h"
  21. #endif
  22.  
  23. #ifndef __APPLICATION__
  24. #include "Application.h"
  25. #endif
  26.  
  27. #ifndef __ABOUT__
  28. #include "About.h"
  29. #endif
  30.  
  31.  
  32. extern SpriteTestPtr gSpriteTestP;
  33.  
  34.  
  35. ///--------------------------------------------------------------------------------------
  36. // DisplayAboutBox
  37. ///--------------------------------------------------------------------------------------
  38.  
  39. void DisplayAboutBox(void)
  40. {
  41.     OSErr                 err;
  42.     GrafPtr             savePort;
  43.     DialogPtr             aboutDialogP;
  44.     SpriteWorldPtr         spriteWorldP = NULL;
  45.     SpriteLayerPtr         spriteLayerP = NULL;
  46.     SpritePtr             earthSpriteP = NULL; 
  47.     SpritePtr            shadowSpriteP = NULL;
  48.     Rect                 itemRect, moveBoundsRect;
  49.     short                 itemHit, itemType;
  50.     Handle                itemHandle;
  51.     ModalFilterUPP        spriteWorldDialogFilter;
  52.     UserItemUPP            procForVersUserItem;
  53.  
  54.     aboutDialogP = GetNewDialog(kAboutDialogID, NULL, (WindowPtr)-1L);
  55.     
  56.     procForVersUserItem = NewUserItemProc(DrawVersionProc);
  57.     GetDialogItem(aboutDialogP, 6, &itemType, &itemHandle, &itemRect);
  58.     SetDialogItem(aboutDialogP, 6, itemType, (Handle)procForVersUserItem, &itemRect);
  59.     
  60.     if (aboutDialogP != NULL)
  61.     {
  62.         GetPort(&savePort);
  63.         SetPort(aboutDialogP);
  64.  
  65.             // create the sprite world
  66.         err = SWCreateSpriteWorldFromWindow(&spriteWorldP, (CWindowPtr)aboutDialogP, NULL, NULL, 0);
  67.     
  68.         if (err == noErr)
  69.         {    
  70.                 // create the sprite layer
  71.             err = SWCreateSpriteLayer(&spriteLayerP);
  72.         }
  73.  
  74.         if (err == noErr)
  75.         {
  76.             err = SWCreateSpriteFromCicnResource(
  77.                 spriteWorldP,
  78.                 &earthSpriteP, 
  79.                 NULL, 
  80.                 kEarthCIconID, 
  81.                 1, 
  82.                 kRegionMask);
  83.         }
  84.  
  85.         if (err == noErr)
  86.         {
  87.             err = SWCreateSpriteFromCicnResource(
  88.                 spriteWorldP, 
  89.                 &shadowSpriteP, 
  90.                 NULL, 
  91.                 kShadowCIconID,
  92.                 kNumberOfShadowFrames, 
  93.                 kRegionMask);
  94.         }
  95.  
  96.         if (err == noErr)
  97.         {
  98.             shadowSpriteP->userData = (long)earthSpriteP;
  99.  
  100.             SWAddSprite(spriteLayerP, shadowSpriteP);
  101.             SWAddSprite(spriteLayerP, earthSpriteP);
  102.             SWAddSpriteLayer(spriteWorldP, spriteLayerP);
  103.  
  104.             GetDItemRect(aboutDialogP, 4, &itemRect);
  105.             moveBoundsRect = aboutDialogP->portRect;
  106.             moveBoundsRect.bottom = itemRect.top + ((itemRect.bottom-itemRect.top)/2);
  107.  
  108.                 // set the sprite’s movement characteristics
  109.             SWSetSpriteMoveBounds(earthSpriteP, &moveBoundsRect);
  110.             SWSetSpriteMoveDelta(earthSpriteP, 0, kInitialSpeed);
  111.             SWSetSpriteMoveProc(earthSpriteP, EarthMoveProc);
  112.             SWSetSpriteMoveTime(earthSpriteP, 55);
  113.             SWSetSpriteMoveTime(shadowSpriteP, -1);        // never move
  114.             SWSetSpriteFrameTime(shadowSpriteP, 0);
  115.             SWSetSpriteFrameRange(shadowSpriteP, 0, 4);
  116.             SWSetSpriteFrameProc(shadowSpriteP, ShadowFrameProc);
  117.             SWSetCurrentFrameIndex(shadowSpriteP, 2);
  118.  
  119.                 // set the sprite’s initial location
  120.             SWSetSpriteLocation(shadowSpriteP, itemRect.left, itemRect.top);
  121.             GetDItemRect(aboutDialogP, 5, &itemRect);
  122.             SWSetSpriteLocation(earthSpriteP, itemRect.left, itemRect.top);
  123.  
  124.             ((WindowPeek)aboutDialogP)->refCon = (long)spriteWorldP;
  125.  
  126.             ShowWindow(aboutDialogP);
  127.             SetPort(aboutDialogP);
  128.             DrawDialog(aboutDialogP);
  129.             EraseRect(&itemRect);
  130.             OutlineDefaultButton(aboutDialogP, ok);
  131.  
  132.             SWLockSpriteWorld(spriteWorldP);
  133.  
  134.                 // Copy contents of dialog to SpriteWorld's backFrame
  135.             CopyBits(&aboutDialogP->portBits,
  136.                         (BitMap*)spriteWorldP->backFrameP->framePix,
  137.                         &aboutDialogP->portRect,
  138.                         &aboutDialogP->portRect,
  139.                         srcCopy, NULL);
  140.  
  141.  
  142.             spriteWorldDialogFilter = NewModalFilterProc(AboutDialogFilter);
  143.  
  144.             do
  145.             {
  146.                 ModalDialog(spriteWorldDialogFilter, &itemHit);
  147.             } while (itemHit != ok);
  148.  
  149.             DisposeRoutineDescriptor(spriteWorldDialogFilter);
  150.             DisposeRoutineDescriptor(procForVersUserItem);
  151.             SWUnlockSpriteWorld(spriteWorldP);
  152.         }
  153.  
  154.         DisposeDialog(aboutDialogP);
  155.         SWDisposeSpriteWorld(&spriteWorldP);
  156.  
  157.         SetPort(savePort);
  158.  
  159.         if (err != noErr)
  160.         {
  161.             FatalError(err);
  162.         }
  163.     }
  164.     else
  165.     {
  166.         CantFindResource();
  167.     }
  168. }
  169.  
  170.  
  171. ///--------------------------------------------------------------------------------------
  172. // EarthMoveProc
  173. ///--------------------------------------------------------------------------------------
  174.  
  175. SW_FUNC void EarthMoveProc(
  176.     SpritePtr srcSpriteP)
  177. {
  178.         // gravity
  179.     if (srcSpriteP->vertMoveDelta < (short)kMaxSpeed)
  180.         srcSpriteP->vertMoveDelta++;
  181.  
  182.     SWOffsetSprite( srcSpriteP, 0, srcSpriteP->vertMoveDelta );
  183.         // bounce
  184.     if (srcSpriteP->destFrameRect.bottom > srcSpriteP->moveBoundsRect.bottom)
  185.     {
  186.         srcSpriteP->vertMoveDelta = -srcSpriteP->vertMoveDelta;
  187.         
  188.         SWMoveSprite( srcSpriteP, SWGetSpriteHorizLoc(srcSpriteP), 
  189.             srcSpriteP->moveBoundsRect.bottom -
  190.             (srcSpriteP->curFrameP->frameRect.bottom - 
  191.             srcSpriteP->curFrameP->frameRect.top));
  192.     }
  193. }
  194.  
  195.  
  196. ///--------------------------------------------------------------------------------------
  197. // ShadowFrameProc
  198. ///--------------------------------------------------------------------------------------
  199.  
  200. SW_FUNC void ShadowFrameProc(
  201.     SpritePtr srcSpriteP,
  202.     FramePtr curFrameP,
  203.     long* curFrameIndex)
  204. {
  205.     #pragma unused(curFrameP)
  206.     SpritePtr earthSpriteP = (SpritePtr)srcSpriteP->userData;
  207.     short distanceFromTop = earthSpriteP->destFrameRect.top - srcSpriteP->moveBoundsRect.top;
  208.     short height = earthSpriteP->moveBoundsRect.bottom - earthSpriteP->moveBoundsRect.top;
  209.  
  210.     *curFrameIndex = (long)((distanceFromTop * 5) / height);
  211.  
  212.         // lets not index past the last frame!
  213.         // (even though SpriteWorld will not allow it to happen)
  214.     if (*curFrameIndex > 4L) *curFrameIndex = 4L;
  215. }
  216.  
  217.  
  218. ///--------------------------------------------------------------------------------------
  219. // AboutDialogFilter
  220. ///--------------------------------------------------------------------------------------
  221.  
  222. pascal Boolean AboutDialogFilter(
  223.     DialogPtr aboutDialogP,
  224.     EventRecord *event,
  225.     short *itemHit)
  226. {
  227.     Boolean eventHandled = false;
  228.     SpriteWorldPtr spriteWorldP = (SpriteWorldPtr)((WindowPeek)aboutDialogP)->refCon;
  229.  
  230.     switch (event->what)
  231.     {
  232.         case nullEvent:
  233.         {
  234.                 // run the about box animation
  235.             SWProcessSpriteWorld(spriteWorldP);
  236.             SWAnimateSpriteWorld(spriteWorldP);
  237.  
  238.                 // keep the title animation going!
  239.             SpriteTestIdle(gSpriteTestP);
  240.             break;
  241.         }
  242.  
  243.         case keyDown:
  244.         case autoKey:
  245.         {
  246.             char key = (char)(event->message & charCodeMask);
  247.  
  248.             if (key == kReturnChar || (key == kEnterChar))
  249.             {
  250.                 *itemHit = ok;
  251.                 eventHandled = true;
  252.                 ClickDialogButton(aboutDialogP, ok);
  253.             }
  254.  
  255.             break;
  256.         }
  257.  
  258.         case updateEvt:
  259.         {
  260.             if ((DialogPtr)event->message == aboutDialogP)
  261.             {
  262.                 SetPort(aboutDialogP);
  263.                 BeginUpdate(aboutDialogP);
  264.  
  265.                 SWUpdateSpriteWorld(spriteWorldP, true);
  266.                 
  267.                 EndUpdate(aboutDialogP);
  268.                 eventHandled = true;
  269.             }
  270.             else
  271.             {
  272.                 HandleUpdateEvent((WindowPtr)event->message);
  273.             }
  274.  
  275.             break;
  276.         }
  277.     }
  278.  
  279.     return eventHandled;
  280. }
  281.  
  282. /**********************************  DrawVersionProc  *******/
  283.  
  284. static void AddChar( StringPtr dstStr, UInt8 srcChar);
  285. static void AddString( StringPtr dstStr, StringPtr srcStr);
  286.  
  287. void AddChar( StringPtr dstStr, UInt8 srcChar)
  288. {
  289.     dstStr[ dstStr[0] + 1 ] = srcChar;
  290.     dstStr[0] += 1;
  291. }
  292.  
  293. void AddString( StringPtr dstStr, StringPtr srcStr)
  294. {
  295.     BlockMoveData( srcStr + 1, dstStr + dstStr[0] + 1, srcStr[0] );
  296.     dstStr[0] += srcStr[0];
  297. }
  298.  
  299.  
  300. pascal void DrawVersionProc (DialogPtr theDialog, short itemNo)
  301.  
  302. {
  303.     Rect            itemRect;
  304.     short            itemType;
  305.     Handle            itemHandle;
  306.     unsigned long    versNum;
  307.     unsigned char    major,minor,bug,stage,nonrelease;
  308.     StringPtr        stageStr;
  309.     Str15            numStr;
  310.     Str255            textStr;
  311.     
  312.     GetDialogItem ( theDialog, itemNo, &itemType, &itemHandle, &itemRect );
  313.     
  314.     versNum = SWGetSpriteWorldVersion();
  315.     
  316.     major =         ( versNum >> 24 ) & 0xFF;
  317.     minor =            ( versNum >> 20 ) & 0x0F;
  318.     bug =             ( versNum >> 16 ) & 0x0F;
  319.     nonrelease =     ( versNum >> 0 ) & 0xFF;
  320.  
  321.     switch ( ( versNum >> 8 ) & 0xFF )
  322.     {
  323.         case developStage:    stage = 'd';
  324.                             stageStr = "\p dev ";
  325.                             break;    
  326.         case alphaStage:    stage = 'a';
  327.                             stageStr = "\p alpha ";
  328.                             break;    
  329.         case betaStage:        stage = 'b';
  330.                             stageStr = "\p beta ";
  331.                             break;    
  332.         case finalStage:    stage = 0;
  333.                             break;    
  334.  
  335.         default:            stage = '?';
  336.                             stageStr = "\p unknown ";
  337.                             break;    
  338.     }
  339.     
  340.     textStr[0] = 0;
  341.     AddString( textStr, "\pVersion " );
  342.     NumToString(major,numStr);
  343.     AddString(textStr, numStr);
  344.     AddChar( textStr, '.' );
  345.     AddChar( textStr, '0' + minor );
  346.     if ( bug )
  347.     {
  348.         AddChar( textStr, '.' );
  349.         AddChar( textStr, '0' + bug );
  350.     }
  351.     if ( stage )
  352.     {
  353.         AddString( textStr, stageStr );
  354.         NumToString(nonrelease,numStr);
  355.         AddString( textStr, numStr );
  356.     }
  357.     AddString( textStr, "\p by" );
  358.     
  359.     TextFont(applFont);
  360.     TextSize(9);
  361.  
  362.     MoveTo( (itemRect.left + itemRect.right - StringWidth(textStr)) / 2, itemRect.top + 9 );
  363.     
  364.     DrawString( "\pVersion " );
  365.     
  366.     NumToString(major,numStr);
  367.     DrawString(numStr);
  368.     
  369.     DrawChar( '.' );
  370.     DrawChar( '0' + minor );
  371.     
  372.     if ( bug )
  373.     {
  374.         DrawChar( '.' );
  375.         DrawChar( '0' + bug );
  376.     }
  377.     
  378.     if ( stage )
  379.     {
  380.         ForeColor( blueColor );
  381.     //    DrawChar( stage );
  382.         DrawString( stageStr );
  383.         
  384.         NumToString(nonrelease,numStr);
  385.         DrawString( numStr );
  386.         ForeColor( blackColor );
  387.     }
  388.  
  389.     DrawString( "\p by" );
  390.  
  391.     TextFace( 0 );
  392.     TextFont(systemFont);
  393.     TextSize(12);
  394. }
  395.  
  396.  
  397.